﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
#if !NETCOREAPP
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
    using System.Web.Mvc.Html;

#endif

#if NETCOREAPP
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Kugar.Core.Web;
#endif
using Kugar.MVC.MobileUI.WeUI.Helpers;

namespace Kugar.MVC.MobileUI.WeUI.PageControls.PageCommons
{
    public class PageCommon_Panel:ContainerBase
    {
        private string _title = "";
        private bool _isRenderFooter = false;
        private string _footer = "";
        private string _callOnRefresh = "";
        private string _callOnLoadMore = "";

        public PageCommon_Panel(ViewContext viewContext) : base(viewContext)
        {
            this.Css("weui-panel weui-panel_access");
        }

        /// <summary>
        /// 分组标题
        /// </summary>
        /// <param name="title"></param>
        /// <returns></returns>
        public PageCommon_Panel Title(string title)
        {
            _title = title;

            return this;
        }

        /// <summary>
        /// 使用html输出标题,用于处理当标题显示不是纯文本的情况
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public PageCommon_Panel Title(Func<PageCommon_Panel, HelperResult> html)
        {
            _title = html(this).ToHtmlString();

            return this;
        }

        /// <summary>
        /// 启用panel的下拉刷新的功能
        /// </summary>
        /// <param name="callOnRefresh">下拉刷新时,触发的脚本,不允许为空字符串</param>
        /// <returns></returns>
        public PageCommon_Panel EnablePullToRefresh(string callOnRefresh)
        {
            if(string.IsNullOrWhiteSpace(callOnRefresh))
                throw new ArgumentNullException(nameof(callOnRefresh));

            _callOnRefresh = callOnRefresh;

            return this;
        }

        /// <summary>
        /// 启用滚动加载的功能
        /// </summary>
        /// <param name="callOnLoadMoreTrigger"></param>
        /// <returns></returns>
        public PageCommon_Panel EnableLoadingMore(string callOnLoadMoreTrigger)
        {
            if (string.IsNullOrWhiteSpace(callOnLoadMoreTrigger))
                throw new ArgumentNullException(nameof(callOnLoadMoreTrigger));

            _callOnLoadMore = callOnLoadMoreTrigger;
            return this;
        }

        /// <summary>
        /// 输出footer区域
        /// </summary>
        /// <returns></returns>
        public PanelFooter RenderFooter()
        {
            return new PanelFooter(this, ViewContext);
        }
        
        public override void Dispose()
        {
            if (!_isRenderFooter)
            {
                ViewContext.Writer.WriteLine("</div> <!-- end  weui-panel__bd -->");
            }
            

            ViewContext.Writer.WriteLine("</div> <!-- end  weui-panel-->");
        }

        internal override ContainerBase RenderInternal()
        {
            //if (!string.IsNullOrWhiteSpace(_callOnRefresh))
            //{
            //    this.Data("onpulltorefresh", _callOnRefresh);

            //    this.Css("pullToRefreshCollection");
            //}

            //if (!string.IsNullOrWhiteSpace(_callOnLoadMore))
            //{
            //    this.Data("loadmore", "True");
            //    this.Data("loadmorecallback", _callOnLoadMore);
            //}

            ViewContext.Writer.WriteLine($"<div {_attrs}>");

            if (!string.IsNullOrWhiteSpace(_title))
            {
                ViewContext.Writer.WriteLine($"<div class=\"weui-panel__hd\">{_title}</div>");
            }
            

            ViewContext.Writer.WriteLine($"<div class=\"weui-panel__bd {(string.IsNullOrWhiteSpace(_callOnRefresh)?"": "pullToRefreshCollection")}\" data-onpulltorefresh=\"{_callOnRefresh}\" data-loadmore=\"{!string.IsNullOrWhiteSpace(_callOnLoadMore)}\" data-loadmorecallback=\"{_callOnLoadMore}\">");

            if (!string.IsNullOrWhiteSpace(_callOnRefresh))
            {
                ViewContext.Writer.WriteLine("<div class=\"weui-pull-to-refresh__layer\"><div class=\'weui-pull-to-refresh__arrow\'></div><div class=\'weui-pull-to-refresh__preloader\'></div><div class=\"down\">下拉刷新</div><div class=\"up\">释放刷新</div><div class=\"refresh\">正在刷新</div></div>");
            }
            //ViewContext.Writer.WriteLine($"<div class=\"weui-panel__bd \" >");

            return this;
        }

        public class PanelFooter : ContainerBase
        {
            private PageCommon_Panel _panel = null;

            internal PanelFooter(PageCommon_Panel panel, ViewContext viewContext) : base(viewContext)
            {
                _panel = panel;
            }

            public override void Dispose()
            {
                ViewContext.Writer.WriteLine("</di><!-- end weui-panel__ft -->");
            }

            internal override ContainerBase RenderInternal()
            {
                _panel._isRenderFooter = true;

                ViewContext.Writer.WriteLine("</div> <!-- end  panel__bd-->");

                ViewContext.Writer.WriteLine("<div class=\"weui-panel__ft\">");

                return this;
            }
        }
    }
}